package org.text2diagram.ddl.domain;

import java.util.ArrayList;

import org.text2diagram.ddl.converter.ConverterException;

public class Color {
	
	public enum StandardColor {
		
		RED("red", new Color(255, 0, 0)),
		GREEN("green", new Color(0, 255, 0)),
		BLUE("blue", new Color(0, 0, 255)),
		WHITE("blue", new Color(255, 255, 255));
		
		private String name;
		private Color color;
		
		private StandardColor(String name, Color color) {
			this.name = name;
			this.color = color;
		}
		
		public static Color findStandardColor(String origColorName) throws ConverterException {
			String colorName = origColorName.toLowerCase();
			for (StandardColor color : StandardColor.values()) {
				if (color.getName().equals(colorName))
					return color.getColor();
			}
			//no matching StandardColor found, raise exception with user friendly message
			throw new ConverterException("No matching standard color for name: \"" + origColorName + "\"");
		}

		public Color getColor() {
			return color;
		}

		public String getName() {
			return name;
		}

	}
	
	private int red;
	private int green;
	private int blue;		

	private ArrayList<String> names = new ArrayList<String>();
	
	private Color(int red, int green, int blue) {
		this.red = red;
		this.green = green;
		this.blue = blue;			
	}
	
	@Override
	public String toString() {
		return String.format("Color(RGB: %d,%d,%d)", red, green, blue);
	}
	
	public static Color findColor(String description) throws ConverterException {
		if (description.startsWith("RGB")) { //e.g. "RGB 10,15,0" or "RGB(0,128,0)"
			return parseRGBColorDescription(description);
		} else { //assume standard color like "red" or "Blue"
			return StandardColor.findStandardColor(description);
		}
	}
	
	private static Color parseRGBColorDescription(String origDescription) throws ConverterException {
		try {
			//canonize description
			String description = origDescription.replaceAll("RGB", "")
			.replaceAll(" ", "")
			.replaceAll("/", ",")
			.replaceAll("\\(", "").replaceAll("\\)", "");
			//parse individual components
			String[] components = description.split(",");
			int red = Integer.parseInt(components[0]);
			int green = Integer.parseInt(components[1]);
			int blue = Integer.parseInt(components[2]);
			return new Color(red, green, blue);
		} catch (Exception e) {
			throw new ConverterException("Color description is incorrect: \"" + origDescription + "\"");
		}
	}
	
	public static ArrayList<String> getAllStandardColorNames() {
		ArrayList<String> names = new ArrayList<String>();
		for (StandardColor standardColor : StandardColor.values()) {
			names.add(standardColor.getName());
		}
		return names;
	}

	public ArrayList<String> getNames() {
		return names;
	}

	public int getRed() {
		return red;
	}

	public int getGreen() {
		return green;
	}

	public int getBlue() {
		return blue;
	}

	public void setRed(int red) {
		this.red = red;
	}

	public void setGreen(int green) {
		this.green = green;
	}

	public void setBlue(int blue) {
		this.blue = blue;
	}
}
